package com.mz.mattress.admin.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.CouponQueryReq;
import com.mz.mattress.admin.common.Holder;
import com.mz.mattress.admin.service.CouponService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Coupon;
import com.mz.mattress.api.entity.CouponSendRecord;
import com.mz.mattress.api.jwt.AdminJwt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

@Slf4j
@RestController
@RequestMapping(Constants.ADMIN_PATH + "coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("/listPage")
    public Result<IPage<Coupon>> listPage(CouponQueryReq req) {
        return couponService.listPage(req);
    }

    @GetMapping(value = "/{id}")
    public Result<Coupon> detail(@PathVariable Integer id) {
        return couponService.getById(id);
    }

    @PostMapping
    public Result add(@RequestBody Coupon coupon) {
        return couponService.add(coupon);
    }

    @PutMapping
    public Result update(@RequestBody Coupon coupon) {
        return couponService.updateById(coupon);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return couponService.deleteByIds(ids);
    }

    @PostMapping("/sendCoupon")
    public Result sendCoupon(@RequestBody CouponSendRecord record) {

        AdminJwt adminJwt = Holder.ADMIN_JWT_THREAD_LOCAL.get();
        record.setUserId(adminJwt.getUserId());
        record.setUserName(adminJwt.getUsername());
        return couponService.sendCoupon(record);

    }

}
